//
//  WWFreshCollCell.m
//  Shopping
//
//  Created by 孙博 on 16/8/20.
//  Copyright © 2016年 张湾湾. All rights reserved.
//

#import "WWFreshCollCell.h"
#import <UIImageView+WebCache.h>
#import <SVProgressHUD.h>
#import "PurchaseCarAnimationTool.h"
@interface WWFreshCollCell ()
// 图片
@property (nonatomic, weak)UIImageView *imgView;
// 名字
@property (nonatomic, weak)UILabel *nameLabel;
// 精选
@property (nonatomic, weak)UILabel *jingxuan;
// 买一送一
@property (nonatomic, weak)UILabel *present;
// 规格
@property (nonatomic, weak)UILabel *speices;
// 价格
@property (nonatomic, weak)UILabel *price;
// 减少 按钮
@property (nonatomic, weak)UIButton *reduce;
// 增加按钮
@property (nonatomic, weak)UIButton *increase;
// 减号
@property (nonatomic ,weak)UIButton *subBtn;
// 数量label // 选中的商品数量
@property (nonatomic, weak)UILabel *lb_count;
// 加号
@property (nonatomic, weak)UIButton *addBtn;
@end

@implementation WWFreshCollCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

#pragma
#pragma mark - setter方法 -
- (void)setModel:(WWFreshSellModel *)model{
    _model = model;
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.img]];
    self.nameLabel.text = model.name;
    if (model.pm_desc) {
        self.present.hidden = NO;
    }else{
        self.present.hidden = YES;
    }
    if (model.goodsCount > 0) {
        self.lb_count.hidden = NO;
        self.subBtn.hidden = NO;
        self.lb_count.text = [NSString stringWithFormat:@"%ld",model.goodsCount];
    }else{
        self.lb_count.hidden = YES;
        self.subBtn.hidden = YES;
    }
    self.speices.text = model.specifics;
    self.price.attributedText = [self getAttributedTextWithMarketPrice:model.market_price price:model.price];
}

// 设置富文本文字
- (NSAttributedString *)getAttributedTextWithMarketPrice:(NSString *)marketPrice price:(NSString *)price{
    // 富文本
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"$%@",price] attributes:@{NSForegroundColorAttributeName : [UIColor redColor],NSFontAttributeName : [UIFont systemFontOfSize:13]}];
    NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@" $%@",marketPrice] attributes:@{NSForegroundColorAttributeName : [UIColor grayColor],NSStrikethroughStyleAttributeName :@(NSUnderlinePatternSolid | NSUnderlineStyleSingle),NSFontAttributeName : [UIFont systemFontOfSize:13]}];
    
    [attributedString appendAttributedString:attributedString1];
    return attributedString;
}
#pragma
#pragma mark - 按钮点击 -
- (void)subCount{
//    NSLog(@"减号");
    if (self.model.goodsCount == 1) {
        
        self.model.goodsCount --;
        self.lb_count.hidden = YES;
        self.subBtn.hidden = YES;
        return;
    }
    self.model.goodsCount --;
    self.lb_count.text = [NSString stringWithFormat:@"%ld",self.model.goodsCount];
}

- (void)addCount{
//    NSLog(@"加号");
//    NSLog(@"%d",self.model.number);
    if (self.model.goodsCount == self.model.number ) {
        [SVProgressHUD setMinimumDismissTimeInterval:2];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
        [SVProgressHUD showImage:[[UIImage imageNamed:@"v2_orderSuccess"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  status:@"asdfasfa"];
        return;
    }

    self.model.goodsCount ++;
    self.lb_count.text = [NSString stringWithFormat:@"%ld",self.model.goodsCount];
    self.lb_count.hidden = NO;
    self.subBtn.hidden = NO;
    [self animationWhileClickAdd];
}

// 点击加号按钮动画
- (void)animationWhileClickAdd{
    
    // 将图片的rect 转为相对于屏幕的
    CGRect rect = [self.imgView convertRect:self.imgView.bounds toView:nil];

    [[PurchaseCarAnimationTool shareTool]startAnimationandView:self.imgView andRect:rect andFinisnRect:CGPointMake(ScreenWidth/4*2.5, ScreenHeight-49) andFinishBlock:^(BOOL finisn){
        // 发送通知 改变 tabBar上的内容
        //        UIView *tabbarBtn = self.tabBarController.tabBar.subviews[3];
        //        [PurchaseCarAnimationTool shakeAnimation:tabbarBtn];
        NSLog(@"发送通知 改变 tabBar上的内容");
    }];
    
}

#pragma
#pragma mark - 设置UI -
- (void)setupUI{
    UIImageView *imgView =[[UIImageView alloc]init];
    self.imgView = imgView;
    [self.contentView addSubview:imgView];
    
    // 姓名
    UILabel *nameLable = [[UILabel alloc]init];
    self.nameLabel = nameLable;
    [self.contentView addSubview:nameLable];
    nameLable.textColor = [UIColor blackColor];
    nameLable.font = [UIFont systemFontOfSize:15];
    
    // 精选
    UILabel *selectedLable = [[UILabel alloc]init];
    self.jingxuan = selectedLable;
    selectedLable.font = [UIFont systemFontOfSize:14];
    selectedLable.text = @" 精 选 ";
    selectedLable.textColor = [UIColor redColor];
    selectedLable.backgroundColor = [UIColor whiteColor];
    selectedLable.layer.cornerRadius = 5;
    selectedLable.layer.masksToBounds = YES;
    // 描边
    selectedLable.layer.shadowColor = [UIColor blueColor].CGColor;
    selectedLable.layer.shadowOffset = CGSizeMake(1, 1);
    selectedLable.layer.shadowOpacity = 0.5;
    selectedLable.layer.shadowRadius = 2;
    [self.contentView addSubview:selectedLable];
    
    //买一送一
    UILabel *presentLabel = [UILabel new];
    self.present = presentLabel;
    [self.contentView addSubview:presentLabel];
    presentLabel.font = [UIFont systemFontOfSize:14];
    presentLabel.text = @"买一送一";
    presentLabel.backgroundColor = [UIColor redColor];
    presentLabel.textColor = [UIColor whiteColor];
    presentLabel.layer.cornerRadius = 5;
    presentLabel.layer.masksToBounds = YES;
    // 规格
    UILabel *speciesLabel = [UILabel new];
    self.speices = speciesLabel;
    [self.contentView addSubview:speciesLabel];
    speciesLabel.textColor = [UIColor grayColor];
    speciesLabel.font = [UIFont systemFontOfSize:14];
    // 价格
    UILabel *priceLable = [[UILabel alloc]init];
    self.price = priceLable;
    [self.contentView addSubview:priceLable];
    // 减号
    UIButton *subBtn = [[UIButton alloc]init];
    self.subBtn = subBtn;
    subBtn.hidden = YES;
    [self.contentView addSubview:subBtn];
    [subBtn addTarget:self action:@selector(subCount) forControlEvents:UIControlEventTouchUpInside];
    [subBtn setImage:[UIImage imageNamed:@"v2_reduce"] forState:UIControlStateNormal];
    
    // 数量label
    UILabel *lb_count = [[UILabel alloc]init];
    self.lb_count = lb_count;
    lb_count.hidden = YES;
    [self.contentView addSubview:lb_count];
    self.lb_count.font = [UIFont systemFontOfSize:15];
    self.lb_count.textAlignment = NSTextAlignmentCenter;
    self.lb_count.text = @"10";
    [lb_count sizeToFit];
    self.lb_count.textColor = [UIColor blackColor];
    // 加号
    UIButton *addBtn = [[UIButton alloc]init];
    self.addBtn = addBtn;
    [self.contentView addSubview:addBtn];
    [addBtn addTarget:self action:@selector(addCount) forControlEvents:UIControlEventTouchUpInside];
    [addBtn setImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
//    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.imgView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height * 0.7);
    // name
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imgView.bottom);
        make.leading.equalTo(self.contentView).offset(5);
    }];
    // 精选
    [self.jingxuan sizeToFit];
    [self.jingxuan makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.bottom);
        make.leading.equalTo(self.contentView).offset(5);
    }];
    // 买一送一
    [self.present sizeToFit];
    [self.present makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.jingxuan.trailing).offset(5);
        make.top.equalTo(self.nameLabel.bottom);
    }];
    // 规格
    [self.speices makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).offset(5);
        make.top.equalTo(self.present.bottom);
    }];
    // 价格
    [self.price makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).offset(5);
        make.top.equalTo(self.speices.bottom);
    }];
    // 加号
    [self.addBtn sizeToFit];
    [self.addBtn makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView).offset(-5);
        make.bottom.equalTo(self.contentView.bottom);
    }];
    // 数量
//    [self.lb_count sizeToFit];
    [self.lb_count makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.addBtn.leading).offset(-3);
        make.centerY.equalTo(self.addBtn.centerY);
    }];
    // 减号
    [self.subBtn sizeToFit];
    [self.subBtn makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.lb_count.leading).offset(-3);
        make.centerY.equalTo(self.addBtn.centerY);
    }];
}


@end
